// 局部混入mixin_post.js文件

export default {
	data() {
		return {
			pageSize: 10, // 每页数据量
			pageIndex0: 1, // 第几页
			pageCount0: 0, // 总页数
			list0: [], // 列表数据
			loadStatus0: 'loadmore',  // 加载状态
		}
	},
	onPullDownRefresh() {
		this.getList(0)
	},
	onReachBottom() {
		this.getList(1)
	},
	methods: {
		// 将图片地址字符串转为数组
		getArrImage(post) {
			if (!post.images) return []
			let arr = post.images.split(",")
			return arr
		},
		
		// 直接点赞，适用于详情
		postPraise(id) {
			if (!this.userId) {
				// 跳转登录
				this.gotoLogin()
			} else {
				this.$api.PostPraise({
					worldId: id
				}).then(res => {
					console.log('postPraise-success', res)
					if (res.data.code == 200) {
						if (this.post.praiseStatus) {
							this.post.praiseStatus = 0
							this.post.praiseCount -= 1
						} else{
							this.post.praiseStatus = 1
							this.post.praiseCount += 1
						}
						this.updateBackPraise({
							id: id,
							praiseStatus: this.post.praiseStatus
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					console.log('postPraise-fail', err)
				})
			}
		},
		
		// 匿名评论提示
		anonymousTip(isRealnameComment) {
			if (isRealnameComment) {
				uni.showToast({
					icon: 'none',
					title: '本帖子不能匿名评论！'
				})
			} else if (!this.isAuthent) {
				uni.showToast({
					icon: 'none',
					title: '未实名不能评论！',
					success: () => {
						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/subPackage/user/my_userinfo_bind/my_userinfo_bind?type=${2}`
							})
						}, 300)
					}
				})
			}
		},
		
		// 提交评论
		addPostComment() {
			if (!this.userId) {
				// 跳转登录
				this.gotoLogin()
			} else if (!this.isAuthent) { // 如果用户未实名
				uni.showModal({
					content: `未实名不能评论！确定前往实名？`,
					confirmColor: this.primaryColor,
					success: (res) => {
						if (res.confirm) {
							uni.navigateTo({
								url: `/pages/subPackage/user/my_userinfo_bind/my_userinfo_bind?type=${2}`
							})
						}
					}
				})
			} else if (!this.content) {
				uni.showToast({
					icon: 'none',
					title: '评论内容不能为空！'
				})
			} else {
				this.$api.AddPostComment({
					content: this.content,
					worldId: this.post.id,
					isAnonymous: this.anonymous?1:0
				}).then(res => {
					console.log('addPostComment-success', res)
					if (res.data.code == 200) {
						this.content = ''
						// 将评论插入列表
						let comment = {
							...res.data.data,
							showReply: false,
							replyList: [],
							pageSize: 10,
							pageIndex: 1
						}
						this.comments.splice(0, 0, comment)
						this.cancelInput()
						uni.showToast({
							icon: 'none',
							title: '评论成功'
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					console.log('addPostComment-fail', err)
				})
			}
		},
		
		// 提交回复
		addPostReply() {
			let id = this.comments[this.commentIndex].id
			let replyuserId = this.comments[this.commentIndex].userId
			if (!this.userId) {
				// 跳转登录
				this.gotoLogin()
			} else if (!this.isAuthent) { // 如果用户未实名
				uni.showModal({
					content: `未实名不能回复！确定前往实名？`,
					confirmColor: this.primaryColor,
					success: (res) => {
						if (res.confirm) {
							uni.navigateTo({
								url: `/pages/subPackage/user/my_userinfo_bind/my_userinfo_bind?type=${2}`
							})
						}
					}
				})
			} else if (!this.content) {
				uni.showToast({
					icon: 'none',
					title: '回复内容不能为空！'
				})
			} else {
				this.$api.AddPostReply({
					content: this.content,
					userId: replyuserId,
					commentId: id, // 评论id
					isAnonymous: this.anonymous?1:0
				}).then(res => {
					console.log('addPostReply-success', res)
					if (res.data.code == 200) {
						this.content = ''
						let item = this.comments[this.commentIndex]
						item.replyCount = item.replyCount + 1
						if (item.showReply && (item.replyList.length/item.pageSize < item.pageSize)) {
							// 原来是展开且当前页的回复数量不超过每页最大值
							item.replyList.push(res.data.data)
						} else if (!item.showReply) { // 原来是收起
							this.getPostReplyList(item, 0)
							item.showReply = true
						}
						this.cancelInput()
						uni.showToast({
							icon: 'none',
							title: '回复成功'
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					console.log('addPostReply-fail', err)
				})
			}
		},
		
		// 获取评论列表
		getPostCommentList(condition) {
			// 判断情况， 0新获取 1分页
			if (condition==0) {
				this.pageIndex = 1
			} else if (this.pageIndex >= this.pageCount) {
				return this.loadStatus = 'nomore'
			} else {
				this.pageIndex += 1
			}
			this.loadStatus = 'loading'
			let params = {
				current: this.pageIndex,
				limit: this.pageSize,
				id: this.id
			}
			if (this.userId) params['userId'] = this.userId
			this.$api.GetPostCommentList(params).then(res => {
				console.log('GetPostCommentList-success', res)
				if(res.data.code == 200) {
					let data = res.data.data.records.map(item => {
						return {
							...item,
							showReply: false,
							replyList: [],
							pageSize: 10,
							pageIndex: 1
						}
					})
					if (this.pageIndex == 1) {
						this.comments = data
						this.pageCount = res.data.data.pages
					} else {
						this.comments = [...this.comments, ...data]
					}
					if (this.pageIndex < this.pageCount) {
						this.loadStatus = 'loadmore'
					} else {
						this.loadStatus = 'nomore'
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
				}
			}).catch(err => {
				console.log('GetPostCommentList-fail', err)
			}).finally(() => {
				
			})
		},
		// 获取动态评论回复列表
		getPostReplyList(item, condition) {
			// 判断情况， 0新获取 1分页
			if (condition==0) {
				item.pageIndex = 1
			} else if (item.replyList.length < item.replyCount) {
				item.pageIndex += 1
			}
			let params = {
				current: item.pageIndex,
				limit: item.pageSize,
				commentId: item.id
			}
			if (this.userId) params['userId'] = this.userId
			this.$api.GetPostReplyList(params).then(res => {
				console.log('GetPostReplyList-success', res)
				if(res.data.code == 200) {
					item.replyList = [...item.replyList, ...res.data.data.records]
				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
				}
			}).catch(err => {
				console.log('GetPostReplyList-fail', err)
			}).finally(() => {
				
			})
		},
		// 展示收起回复
		showReply(i) {
			let item = this.comments[i]
			if (item.showReply) { // 原来是展开
				item.replyList.length = 0
				item.showReply = false
			} else { // 原来是收起
				this.getPostReplyList(item, 0)
				item.showReply = true
			}
		},
		
		// 获取详情
		getPostDetail() {
			let params = {
				id: this.id
			}
			if (this.userId) params['userId'] = this.userId
			this.$api.GetPostDetail(params).then(res => {
				console.log('GetPostDetail-success', res)
				if (res.data.code == 200) {
					this.post = res.data.data
				} else {
					uni.navigateBack()
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
				}
			}).catch(err => {
				console.log('GetPostDetail-fail', err)
				uni.navigateBack()
			}).finally(() => {
				uni.stopPullDownRefresh()
			})
		},
		
		// 获取帖子列表
		getList(condition) {
			if (this.pageType == 0) { // 首页
				if (condition == 0) {
					this.pageIndex0 = 1
				} else if (this.pageIndex0 >= this.pageCount0) {
					return this.loadStatus0 = 'nomore'
				} else {
					this.pageIndex0 += 1
				}
				this.loadStatus0 = 'loading'
			} else {
				if (condition == 0) {
					this[`pageIndex${this.type}`] = 1
				} else if (this[`pageIndex${this.type}`] >= this[`pageCount${this.type}`]) {
					return this[`loadStatus${this.type}`] = 'nomore'
				} else {
					this[`pageIndex${this.type}`] += 1
				}
				this[`loadStatus${this.type}`] = 'loading'
			}
			
			// 发送请求
			let params = {
				current: this[`pageIndex${this.type}`],
				limit: this.pageSize
			}
			switch (this.pageType){
				case 0: // 首页
					params['schoolId'] = this.collegeId
					params['typeId'] = this.tabList[this.type].id
					if (this.userId) params['userId'] = this.userId
					this.$api.GetPostList(params).then(res => {
						console.log('GetPostList-success', res)
						if(res.data.code == 200) {
							// 请求成功后
							if (this.pageIndex0 == 1) {
								this.list0 = res.data.data.records
								this.pageCount0 = res.data.data.pages
							} else {
								this.list0 = [...this.list0, ...res.data.data.records]
							}
							if (this.pageIndex0 < this.pageCount0) {
								this.loadStatus0 = 'loadmore'
							} else {
								this.loadStatus0 = 'nomore'
							}
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
						}
					}).catch(err => {
						console.log('GetPostList-fail', err)
					}).finally(() => {
						uni.stopPullDownRefresh()
					})
					break;
				case 1: // 我的帖子
					params['status'] = this.type-1
					this.$api.GetMyPostList(params).then(res => {
						console.log('GetMyPostList-success', res)
						if(res.data.code == 200) {
							// 请求成功后
							this.dataHandel(res.data.data)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
						}
					}).catch(err => {
						console.log('GetMyPostList-fail', err)
					}).finally(() => {
						uni.stopPullDownRefresh()
					})
					break;
				case 2: // 我的点赞
					this.$api.GetLikePostList(params).then(res => {
						console.log('GetLikePostList-success', res)
						if(res.data.code == 200) {
							// 请求成功后
							this.dataHandel(res.data.data)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg
							})
						}
					}).catch(err => {
						console.log('GetLikePostList-fail', err)
					}).finally(() => {
						uni.stopPullDownRefresh()
					})
					break;
				default:
					break;
			}
		},
		
		// 请求成功后
		dataHandel(data) {
			if (this[`pageIndex${this.type}`] == 1) {
				this[`list${this.type}`] = data.records
				this[`pageCount${this.type}`] = data.pages
			} else {
				this[`list${this.type}`] = [...this[`list${this.type}`], ...data.records]
			}
			if (this[`pageIndex${this.type}`] < this[`pageCount${this.type}`]) {
				this[`loadStatus${this.type}`] = 'loadmore'
			} else {
				this[`loadStatus${this.type}`] = 'nomore'
			}
		},
		
		// 格式化点赞和评论数量
		formatPraisesComments(value, tip) {
			if(value >= 10000) {
				let newValue = value/10000
				return newValue.toFixed(1)+'w'
			}else if(value >= 1000) {
				let newValue = value/1000
				return newValue.toFixed(1)+'k'
			}else if(value > 0) {
				return value
			}else{
				return tip
			}
		},
	}
}